/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.helper.EncCommonHelper;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.Output;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class OutputListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"OutputList.xls\"");
		HSSFFont font = workbook.createFont();
		font.setFontHeightInPoints(10);
		font.setColor(10);
		font.setFontName("黑体");
		font.setBoldweight(700);
		HSSFCellStyle cellStyle = workbook.createCellStyle();
		cellStyle.setFont(font);
		cellStyle.setAlignment(2);
		cellStyle.setBorderBottom(1);
		cellStyle.setWrapText(true);
		cellStyle.setFillForegroundColor(22);
		cellStyle.setFillPattern(1);
		HSSFSheet sheet = workbook.createSheet("list");
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		r.createCell(12);
		r.createCell(13);
		r.createCell(14);
		r.createCell(15);
		r.createCell(16);
		c.setCellValue(new HSSFRichTextString("产量一览表"));
		sheet.addMergedRegion(new Region(0, 0, 0, 16));
		r = sheet.createRow(1);
		r.createCell(0).setCellValue(new HSSFRichTextString("制作日期"));
		r.createCell(1).setCellValue(new HSSFRichTextString("业务单号"));
		r.createCell(2).setCellValue(new HSSFRichTextString("订货单位"));
		r.createCell(3).setCellValue(new HSSFRichTextString("项目类型"));
		r.createCell(4).setCellValue(new HSSFRichTextString("内容"));
		r.createCell(5).setCellValue(new HSSFRichTextString("材质"));
		r.createCell(6).setCellValue(new HSSFRichTextString("规格"));
		r.createCell(7).setCellValue(new HSSFRichTextString("计量单位"));
		r.createCell(8).setCellValue(new HSSFRichTextString("数量"));
		r.createCell(9).setCellValue(new HSSFRichTextString("制作要求"));
		r.createCell(10).setCellValue(new HSSFRichTextString("工作中心"));
		r.createCell(11).setCellValue(new HSSFRichTextString("工序"));
		r.createCell(12).setCellValue(new HSSFRichTextString("制作人"));
		r.createCell(13).setCellValue(new HSSFRichTextString("占比"));
		r.createCell(14).setCellValue(new HSSFRichTextString("备注"));
		r.createCell(15).setCellValue(new HSSFRichTextString("登记人"));
		r.createCell(16).setCellValue(new HSSFRichTextString("登记时间"));
		List list = (List) model.get("list");
		List wcList = (List) model.get("workCenterList");
		List mtList = (List) model.get("makeTypeList");
		List proList = (List) model.get("processList");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat longSf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			Output o = (Output) objs[0];
			BusinessBillItem item = (BusinessBillItem) objs[1];
			String unit;
			if (item.getPriceType().equals("AREA")) {
				unit = "平方米";
			} else if (item.getPriceType().equals("LENGTH")) {
				if (item.getLengType().equals("M")) {
					unit = "米";
				} else {
					unit = "公分";
				}
			} else {
				unit = item.getUnit();
			}

			BigDecimal planAmount;
			if (!item.getPriceType().equals("AREA")
					&& !item.getPriceType().equals("LENGTH")) {
				planAmount = item.getAmount();
			} else {
				planAmount = item.getSquareNum();
			}

			String estName = "";
			if (!EncCommonHelper.checkPurePrivilege(request, "NO_CLIE_NAME")) {
				estName = (String) objs[3];
				if (objs[5] != null && !objs[5].toString().equals("")) {
					estName = estName + "(" + objs[5] + ")";
				}
			}

			String spe = "";
			if (item.getSpecification() != null) {
				spe = item.getSpecification();
			}

			if (item.getPriceType().equals("AREA")
					|| item.getPriceType().equals("LENGTH")) {
				spe = spe + " x "
						+ EncCommonHelper.parseBigDecimal(item.getAmount());
			}

			r = sheet.createRow(os + 2);
			r.createCell(0).setCellValue(
					new HSSFRichTextString(sf.format(o.getMakeDate())));
			r.createCell(1).setCellValue(
					new HSSFRichTextString((String) objs[2]));
			r.createCell(2).setCellValue(new HSSFRichTextString(estName));
			r.createCell(3).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							mtList, item.getMakeTypeId())));
			r.createCell(4).setCellValue(
					new HSSFRichTextString(item.getFinishedContent()));
			r.createCell(5).setCellValue(
					new HSSFRichTextString(item.getProName()));
			r.createCell(6).setCellValue(new HSSFRichTextString(spe));
			r.createCell(7).setCellValue(new HSSFRichTextString(unit));
			r.createCell(8).setCellValue(planAmount.doubleValue());
			r.createCell(9).setCellValue(
					new HSSFRichTextString(item.getMakeRequire()));
			r.createCell(10).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							wcList, item.getWcId())));
			r.createCell(11).setCellValue(
					new HSSFRichTextString(EncCommonHelper.getSimpleObjectName(
							proList, o.getProcessId())));
			r.createCell(12).setCellValue(
					new HSSFRichTextString(o.getMakerName()));
			r.createCell(13).setCellValue(o.getPercent().doubleValue());
			r.createCell(14).setCellValue(new HSSFRichTextString(o.getMark()));
			r.createCell(15).setCellValue(
					new HSSFRichTextString(o.getCreaterName()));
			r.createCell(16).setCellValue(
					new HSSFRichTextString(longSf.format(o.getCreateTime())));
		}

		ServletOutputStream arg23 = response.getOutputStream();
		workbook.write(arg23);
		arg23.flush();
		arg23.close();
	}
}